package com.sfilyh.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {
	
	/**
	 * 创建文件
	 * @param is
	 * @param outfile
	 * @throws IOException
	 */
	public static void createFile(InputStream is,File outfile) throws IOException{
		String path =  outfile.getPath().substring(0, outfile.getPath().lastIndexOf(File.separator));
		File dirPath = new File(path);
		if(!dirPath.exists()){
			dirPath.mkdirs();
		}
		try {
			BufferedInputStream bis = new BufferedInputStream(is);
			FileOutputStream fos = new FileOutputStream(outfile);
			byte[] buffer = new byte[1024];
			int len ;
			while( (len = bis.read(buffer)) > 0 ){
				fos.write(buffer, 0, len);
			}
			fos.close();
			bis.close();			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}				
	}
}
